#include <time.h>
#include <sys/time.h>

#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <sys/stat.h>
#include <sys/types.h>

int saveFileName(char *filename){
	
	//time_t curtime = time(NULL);
  	//char filename[30] = { 0 };
	char str_dir[128] = {0};

        struct timeval tv;

        gettimeofday(&tv, NULL);

  	struct tm* local_time = localtime(&tv.tv_sec);
  	strftime(filename, 128, "%Y%m%d%H%M%S", local_time);
        sprintf(filename,"%s%03d",filename,tv.tv_usec/1000);
	//printf("%d %s\n ",strlen(filename),filename);
	sprintf(str_dir,"/media/ubuntu/bae/%s",filename);
	mkdir(str_dir,0007);
  	FILE *fp;
  	fp = fopen("filenameTemp.txt","wb");
  	while(fp==NULL){
  		usleep(30);
  		fp = fopen("filenameTemp.txt","wb");
  	} 
        sprintf(filename,"%s\n",filename);
  	fwrite(filename,strlen(filename),1,fp);
  	//fwrite("\n",1,1,fp);
  	fclose(fp);
  	return 1;
}

int main(int argc, char * argv[])
{
    char buff[128];
    saveFileName(buff);
}

